import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView,
    },
    {
      path: '/about',
      name: 'about',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../views/AboutView.vue'),
    },
    {
      path: '/Login',
      name: 'Login',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../views/Meng/Login.vue'),
    },
  ],
})
// BAD
//每个【路由】之前
//每次调用路由之间，都会执行方法【参数1表示要打开的网页地址，参数2现在所在的网页地址，参数3用于跳转页面】
router.beforeEach((to, from, next) => {
  // 如果用户未能验证身份，则 `next` 会被调用两次
  //判断没有登录是，跳转到登录页面，如果判断是已登录的就放行
 if(localStorage.getItem("isLogin")!="true"&&to.name!="Login")//取出登录的状态判读！=登录状态&&目标地址还不是登录页面
  {
    next({path:'/Login'})//跳转到登录页面(里面是路由对象)
  }
  else{
    next()
  } 
  // debugger
  next()
})
export default router
